package com.gitee.ixtf.lucene

import com.gitee.ixtf.lucene.Jlucene.LUCENE_PATH
import org.apache.lucene.facet.FacetsConfig
import org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyReader
import org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyWriter
import org.apache.lucene.index.*
import org.apache.lucene.store.FSDirectory
import org.apache.lucene.util.IOUtils

abstract class BaseTaxoLucene<T> : BaseLucene<T>() {
  protected abstract val facetsConfig: FacetsConfig
  protected open val taxoWriter by lazy {
    val taxoPath = LUCENE_PATH.resolve("${entityClass.simpleName}_Taxonomy")
    DirectoryTaxonomyWriter(FSDirectory.open(taxoPath))
  }

  override fun close() = IOUtils.close(indexWriter, taxoWriter)

  override fun commit(): Long {
    val commit = indexWriter.commit()
    taxoWriter.commit()
    return commit
  }

  fun taxoReader(): DirectoryTaxonomyReader = DirectoryTaxonomyReader(taxoWriter)
}
